package com.compass.mobile.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import com.compass.mobile.databinding.ActivityOrderDetailsBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.constant.MY_ORDER
import com.compass.mobile.framework.constant.YD_USER_ID
import com.compass.mobile.framework.utils.DateUtils
import com.compass.mobile.framework.utils.QRCodeDealUtils
import com.compass.mobile.models.OrderBean
import com.compass.mobile.ui.viewModel.InquiryViewModel
import dp2px
import setUrlRound
import java.sql.Time

/**
 * Description： 问诊单详情
 * Date:2024/7/3 9:42
 */
class OrderDetailsActivity : BaseVBActivity<ActivityOrderDetailsBinding>() {

    companion object {
        fun start(context: Context, orderId: String?) {
            val intent = Intent(context, OrderDetailsActivity::class.java)
            intent.putExtra("orderId", orderId)
            context.startActivity(intent)
        }
    }

    private val inquiryViewModel: InquiryViewModel by viewModels()

    private var orderId: String? = null
    private var codeUrl: String = MY_ORDER + UserInfoManager.getString(YD_USER_ID)
    override fun initView(savedInstanceState: Bundle?) {
        orderId = intent.getStringExtra("orderId")

        mBinding.imgWxCode.setImageBitmap(
            QRCodeDealUtils.generateQRCodeWithoutMargin(
                codeUrl,
                dp2px(400f),
                dp2px(400f)
            )
        )
    }

    override fun initData() {
        showLoading()
        inquiryViewModel.gePatientOrderDetails(orderId)
        inquiryViewModel.orderDetailsLiveData.observe(this) {
            setDoctor(it)
            dismissLoading()
        }
    }

    private fun setDoctor(orderBean: OrderBean?) {
        orderBean?.let {
            mBinding.tvOrderId.text = "订单编号: ${it.id}"
            mBinding.tvStatus.text = "订单状态: ${UserInfoManager.getOrderTypeString(it.status)}"
            mBinding.tvPatientName.text = "患者姓名: ${it.patientName}"
            mBinding.tvDescription.text = "病情描述: ${it.diseaseDescriptionText}"
            mBinding.tvTime.text =
                "创建时间: ${DateUtils.conversionTime(it.createTime, DateUtils.FORMAT_TWO)}"
            mBinding.tvEndTime.text =
                "结束时间: ${getEndTime(it.consultationEndDate)}"
            mBinding.imgHead.setUrlRound(it.doctorAvatar, dp2px(5f))
            mBinding.tvDoctorName.text = it.doctorName
            mBinding.tvPositionName.text = it.positionName
            mBinding.tvDepartmentName.text = it.departmentName

        }
    }

    private fun getEndTime(endTime: Long?): String {
        endTime?.let {

            return DateUtils.conversionTime(it, DateUtils.FORMAT_TWO)
        }
        return "--"
    }
}